﻿#include "function_blob_9006.h"


void blob9006 (HObject ho_image, HObject *ho_region_rectangle, HObject *ho_region_show,
    HObject *ho_region_error, HTuple hv_threshold, HTuple hv_threshold_error, HTuple hv_dilation_x,
    HTuple hv_dilation_y, HTuple hv_height_min, HTuple hv_height_max, HTuple hv_find_number,
    HTuple hv_empty_x, HTuple hv_empty_y, HTuple hv_empty_max_x, HTuple hv_empty_max_y,
    HTuple hv_empty_number, HTuple hv_empty_size, HTuple *hv_size, HTuple *hv_length)
{

  // Local iconic variables
  HObject  ho_Region, ho_ConnectedRegions1, ho_SelectedRegions;
  HObject  ho_SelectedRegionsUnion, ho_RegionDilationColumn;
  HObject  ho_ConnectedRegions2, ho_ConnectedRegionIntersection2;
  HObject  ho_RegionDilationRow, ho_ConnectedRegionIntersection3;
  HObject  ho_ConnectedRegionIntersection4, ho_ConnectedRegionIntersectionSort;
  HObject  ho_ObjectSelected, ho_ImageReduced, ho_RegionReduced0;
  HObject  ho_ConnectedRegionsEmpty, ho_RegionReduced, ho_RegionUnion;
  HObject  ho_RegionReduced_dilation_x, ho_RegionReduced_dilation_x_connection;
  HObject  ho_FiveEmptyObject, ho_ObjectSelected1, ho_SelectedRegionsEmptyWidth;
  HObject  ho_SelectedRegionsEmptyHeight, ho_SelectedRegionsEmptyWidthHeight;
  HObject  ho_SelectedRegionsEmptyMaxWidth, ho_SelectedRegionsEmptyMaxHeight;
  HObject  ho_SelectedRegionsEmptySize, ho_RegionUnionLength;

  // Local control variables
  HTuple  hv_Number, hv_Index, hv_ProductNumber;
  HTuple  hv_Row11, hv_Column11, hv_Row21, hv_Column21, hv_RegionReduced_dilation_x_connection_number;
  HTuple  hv_Index1, hv_Row12, hv_Column12, hv_Row22, hv_Column22;
  HTuple  hv_height_now, hv_ratio, hv_Area, hv_Row, hv_Column;
  HTuple  hv_NumberNow, hv_Row1, hv_Column1, hv_Row2, hv_Column2;


  Threshold(ho_image, &ho_Region, hv_threshold, 255);

  Connection(ho_Region, &ho_ConnectedRegions1);
  SelectShape(ho_ConnectedRegions1, &ho_SelectedRegions, "area", "and", 100, 10000);



  Union1(ho_SelectedRegions, &ho_SelectedRegionsUnion);
  DilationRectangle1(ho_SelectedRegionsUnion, &ho_RegionDilationColumn, 1, hv_dilation_y);

  Connection(ho_RegionDilationColumn, &ho_ConnectedRegions2);

  SelectShape(ho_ConnectedRegions2, &ho_ConnectedRegionIntersection2, "height", "and",
      hv_height_min, hv_height_max);
  Union1(ho_ConnectedRegionIntersection2, &ho_ConnectedRegionIntersection2);
  DilationRectangle1(ho_ConnectedRegionIntersection2, &ho_RegionDilationRow, hv_dilation_x,
      1);




  Connection(ho_RegionDilationRow, &ho_RegionDilationRow);

  SelectShape(ho_RegionDilationRow, &ho_ConnectedRegionIntersection3, "width", "and",
      500, 700);
  CountObj(ho_ConnectedRegionIntersection3, &hv_Number);
  SortRegion(ho_ConnectedRegionIntersection3, &ho_ConnectedRegionIntersection4, "character",
      "true", "row");
  GenEmptyObj(&ho_ConnectedRegionIntersectionSort);
  {
  HTuple end_val26 = hv_Number;
  HTuple step_val26 = 1;
  for (hv_Index=1; hv_Index.Continue(end_val26, step_val26); hv_Index += step_val26)
  {

    SelectObj(ho_ConnectedRegionIntersection4, &ho_ObjectSelected, hv_Index);
    ReduceDomain(ho_image, ho_ObjectSelected, &ho_ImageReduced);
    Threshold(ho_ImageReduced, &ho_RegionReduced0, hv_threshold_error, 255);
    Connection(ho_RegionReduced0, &ho_ConnectedRegionsEmpty);
    SelectShape(ho_ConnectedRegionsEmpty, &ho_RegionReduced, "area", "and", 30, 99999);
    SelectShape(ho_RegionReduced, &ho_RegionReduced, "width", "and", 5, 99999);
    CountObj(ho_RegionReduced, &hv_ProductNumber);
    Union1(ho_RegionReduced, &ho_RegionUnion);
    SmallestRectangle1(ho_RegionUnion, &hv_Row11, &hv_Column11, &hv_Row21, &hv_Column21);

    if (0 != (int(hv_Row11>10)))
    {
      ConcatObj(ho_ConnectedRegionIntersectionSort, ho_RegionUnion, &ho_ConnectedRegionIntersectionSort
          );
    }

  }
  }



  CountObj(ho_ConnectedRegionIntersectionSort, &hv_Number);





  if (0 != (int(hv_Number>4)))
  {
    hv_Number = 4;
  }



  GenEmptyObj(&(*ho_region_rectangle));
  GenEmptyObj(&(*ho_region_show));
  GenEmptyObj(&(*ho_region_error));
  (*hv_size) = HTuple();
  {
  HTuple end_val62 = hv_Number;
  HTuple step_val62 = 1;
  for (hv_Index=1; hv_Index.Continue(end_val62, step_val62); hv_Index += step_val62)
  {

    SelectObj(ho_ConnectedRegionIntersectionSort, &ho_ObjectSelected, hv_Index);

    Connection(ho_ObjectSelected, &ho_ConnectedRegionsEmpty);
    SelectShape(ho_ConnectedRegionsEmpty, &ho_RegionReduced, "area", "and", 30, 99999);
    SelectShape(ho_RegionReduced, &ho_RegionReduced, "width", "and", 5, 99999);
    Union1(ho_RegionReduced, &ho_RegionReduced);

    ConcatObj((*ho_region_rectangle), ho_ObjectSelected, &(*ho_region_rectangle));


    DilationRectangle1(ho_RegionReduced, &ho_RegionReduced_dilation_x, 1000, 1);
    Connection(ho_RegionReduced_dilation_x, &ho_RegionReduced_dilation_x_connection
        );
    CountObj(ho_RegionReduced_dilation_x_connection, &hv_RegionReduced_dilation_x_connection_number);
    GenEmptyObj(&ho_FiveEmptyObject);
    if (0 != (int(hv_RegionReduced_dilation_x_connection_number>=6)))
    {
      for (hv_Index1=2; hv_Index1<=5; hv_Index1+=1)
      {
        SelectObj(ho_RegionReduced_dilation_x_connection, &ho_ObjectSelected1, hv_Index1);
        ConcatObj(ho_FiveEmptyObject, ho_ObjectSelected1, &ho_FiveEmptyObject);
      }
      Intersection(ho_RegionReduced, ho_FiveEmptyObject, &ho_FiveEmptyObject);

      SmallestRectangle1(ho_FiveEmptyObject, &hv_Row12, &hv_Column12, &hv_Row22,
          &hv_Column22);
      hv_height_now = hv_Row22-hv_Row12;
      hv_ratio = 120.0/hv_height_now;
      ZoomRegion(ho_FiveEmptyObject, &ho_FiveEmptyObject, 1, hv_ratio);
      AreaCenter(ho_FiveEmptyObject, &hv_Area, &hv_Row, &hv_Column);
      ConcatObj((*ho_region_show), ho_FiveEmptyObject, &(*ho_region_show));
    }
    else
    {
      SmallestRectangle1(ho_ObjectSelected, &hv_Row12, &hv_Column12, &hv_Row22, &hv_Column22);
      hv_height_now = hv_Row22-hv_Row12;
      hv_ratio = 120.0/hv_height_now;
      ZoomRegion(ho_ObjectSelected, &ho_ObjectSelected, 1, hv_ratio);
      AreaCenter(ho_ObjectSelected, &hv_Area, &hv_Row, &hv_Column);
      ConcatObj((*ho_region_show), ho_ObjectSelected, &(*ho_region_show));
      AreaCenter(ho_ObjectSelected, &hv_Area, &hv_Row, &hv_Column);
    }





    (*hv_size) = (*hv_size).TupleConcat(hv_Area);

    Connection(ho_RegionReduced, &ho_ConnectedRegionsEmpty);

    SelectShape(ho_ConnectedRegionsEmpty, &ho_SelectedRegionsEmptyWidth, "width",
        "and", hv_empty_x, 99999);

    SelectShape(ho_ConnectedRegionsEmpty, &ho_SelectedRegionsEmptyHeight, "height",
        "and", hv_empty_y, 99999);



    ConcatObj(ho_SelectedRegionsEmptyWidth, ho_SelectedRegionsEmptyHeight, &ho_SelectedRegionsEmptyWidthHeight
        );

    CountObj(ho_SelectedRegionsEmptyWidthHeight, &hv_NumberNow);
    if (0 != (int(hv_NumberNow>=3)))
    {
      ConcatObj(ho_SelectedRegionsEmptyWidthHeight, (*ho_region_error), &(*ho_region_error)
          );
    }
    else
    {
      SelectShape(ho_ConnectedRegionsEmpty, &ho_SelectedRegionsEmptyMaxWidth, "width",
          "and", hv_empty_max_x, 99999);

      SelectShape(ho_ConnectedRegionsEmpty, &ho_SelectedRegionsEmptyMaxHeight, "height",
          "and", hv_empty_max_y, 99999);

      SelectShape(ho_ConnectedRegionsEmpty, &ho_SelectedRegionsEmptySize, "area",
          "and", hv_empty_size, 99999);

      ConcatObj(ho_SelectedRegionsEmptyMaxWidth, (*ho_region_error), &(*ho_region_error)
          );
      ConcatObj(ho_SelectedRegionsEmptyMaxHeight, (*ho_region_error), &(*ho_region_error)
          );
      ConcatObj(ho_SelectedRegionsEmptySize, (*ho_region_error), &(*ho_region_error)
          );
    }

  }
  }

  if (0 != (int(hv_Number>0)))
  {
    Union1((*ho_region_rectangle), &ho_RegionUnionLength);
    SmallestRectangle1(ho_RegionUnionLength, &hv_Row1, &hv_Column1, &hv_Row2, &hv_Column2);
    (*hv_length) = hv_Row2-hv_Row1;
  }
  else
  {
    (*hv_length) = 0;
  }

  return;
}



FunctionScan::FunctionScan(QDialog* parent, QSettings* qsettings, QString section,int camera_index) : QDialog(parent)
{
	

    m_qsettings = qsettings;
    m_section = section;

    //this->setWindowTitle(("Dump"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    setFixedSize(1920*ratio_x,1010*ratio_y);

    m_view = new QtQGraphicsView(this);
    m_view->setGeometry(660 * ratio_x, 0, 1250 * ratio_x, 940 * ratio_y);



    QWidget* p = new QWidget(this);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(220 * ratio_x, 40 * ratio_y);

	QLabel* p_qlabel;
	p_qlabel = new QLabel(this);
	p_qlabel->setText("标准面积");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
	m_qspinbox_standard_size = new QSpinBox();
	m_qspinbox_standard_size->setFixedSize(ButtonSize);
	m_qspinbox_standard_size->setMinimum(0);
	m_qspinbox_standard_size->setMaximum(10000000);
	qgridLayout->addWidget(m_qspinbox_standard_size, index, 1);
	index++;

	p_qlabel = new QLabel();
	p_qlabel->setText("比例");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
	m_qspinbox_ratio = new QDoubleSpinBox();
	m_qspinbox_ratio->setFixedSize(ButtonSize);
	m_qspinbox_ratio->setMinimum(0);
	m_qspinbox_ratio->setMaximum(1);
	qgridLayout->addWidget(m_qspinbox_ratio, index, 1);
	index++;

    
    p_qlabel = new QLabel();
    p_qlabel->setText("块提取灰度阈值");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_threshold_gray = new QSpinBox();
    m_qspinbox_threshold_gray->setFixedSize(ButtonSize);
    m_qspinbox_threshold_gray->setMinimum(1);
    m_qspinbox_threshold_gray->setMaximum(254);
    qgridLayout->addWidget(m_qspinbox_threshold_gray, index, 1);
    index++;

    p_qlabel = new QLabel();
    p_qlabel->setText("堵网提取灰度阈值");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_threshold_error = new QSpinBox();
    m_qspinbox_threshold_error->setFixedSize(ButtonSize);
    m_qspinbox_threshold_error->setMinimum(1);
    m_qspinbox_threshold_error->setMaximum(254);
    qgridLayout->addWidget(m_qspinbox_threshold_error, index, 1);
    index++;


	p_qlabel = new QLabel(this);
    p_qlabel->setText("x方向相连");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_dilation_x = new QSpinBox();
    m_qspinbox_dilation_x->setFixedSize(ButtonSize);
    m_qspinbox_dilation_x->setMinimum(1);
    m_qspinbox_dilation_x->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_dilation_x, index, 1);
	index++;

	p_qlabel = new QLabel();
    p_qlabel->setText("y方向相连");
	p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_dilation_y = new QSpinBox();
    m_qspinbox_dilation_y->setFixedSize(ButtonSize);
    m_qspinbox_dilation_y->setMinimum(1);
    m_qspinbox_dilation_y->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_dilation_y, index, 1);
    index++;


    p_qlabel = new QLabel(this);
    p_qlabel->setText("分块高度最小值");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_height_min = new QSpinBox();
    m_qspinbox_height_min->setFixedSize(ButtonSize);
    m_qspinbox_height_min->setMinimum(1);
    m_qspinbox_height_min->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_height_min, index, 1);
    index++;

    p_qlabel = new QLabel();
    p_qlabel->setText("分块高度最大值");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_height_max= new QSpinBox();
    m_qspinbox_height_max->setFixedSize(ButtonSize);
    m_qspinbox_height_max->setMinimum(1);
    m_qspinbox_height_max->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_height_max, index, 1);
    index++;

//    p_qlabel = new QLabel();
//    p_qlabel->setText("分块找到最小数量");
//    p_qlabel->setFixedSize(ButtonSize);
//    qgridLayout->addWidget(p_qlabel, index, 0);
//    m_qspinbox_find_number= new QSpinBox();
//    m_qspinbox_find_number->setFixedSize(ButtonSize);
//    m_qspinbox_find_number->setMinimum(1);
//    m_qspinbox_find_number->setMaximum(120);
//    qgridLayout->addWidget(m_qspinbox_find_number, index, 1);
//    index++;

    p_qlabel = new QLabel();
    p_qlabel->setText("缺料x标准");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_empty_x= new QSpinBox();
    m_qspinbox_empty_x->setFixedSize(ButtonSize);
    m_qspinbox_empty_x->setMinimum(1);
    m_qspinbox_empty_x->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_empty_x, index, 1);
    index++;

    p_qlabel = new QLabel();
    p_qlabel->setText("缺料y标准");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_empty_y= new QSpinBox();
    m_qspinbox_empty_y->setFixedSize(ButtonSize);
    m_qspinbox_empty_y->setMinimum(1);
    m_qspinbox_empty_y->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_empty_y, index, 1);
    index++;

	p_qlabel = new QLabel();
	p_qlabel->setText("缺料x最大");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
	m_qspinbox_empty_max_x = new QSpinBox();
	m_qspinbox_empty_max_x->setFixedSize(ButtonSize);
	m_qspinbox_empty_max_x->setMinimum(1);
	m_qspinbox_empty_max_x->setMaximum(1000);
	qgridLayout->addWidget(m_qspinbox_empty_max_x, index, 1);
	index++;

	p_qlabel = new QLabel();
	p_qlabel->setText("缺料y最大");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
	m_qspinbox_empty_max_y = new QSpinBox();
	m_qspinbox_empty_max_y->setFixedSize(ButtonSize);
	m_qspinbox_empty_max_y->setMinimum(1);
	m_qspinbox_empty_max_y->setMaximum(1000);
	qgridLayout->addWidget(m_qspinbox_empty_max_y, index, 1);
	index++;

    p_qlabel = new QLabel();
    p_qlabel->setText("缺料面积最大");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_empty_size = new QSpinBox();
    m_qspinbox_empty_size->setFixedSize(ButtonSize);
    m_qspinbox_empty_size->setMinimum(0);
    m_qspinbox_empty_size->setMaximum(1000000);
    qgridLayout->addWidget(m_qspinbox_empty_size, index, 1);
    index++;


    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(ButtonSize);
    m_pushbutton_save_parameter->setText("保存");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &FunctionScan::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);

    index++;
	m_pushbutton_show_orign_image = new QPushButton(this);
    m_pushbutton_show_orign_image->setFixedSize(ButtonSize);
    m_pushbutton_show_orign_image->setText("显示原图");
	connect(m_pushbutton_show_orign_image, &QPushButton::clicked, this, [=]() {
		QImage q_image = matToQImage(m_image);
        m_view->setImage(q_image, "",Qt::red, 20);
		return true;
	});
	qgridLayout->addWidget(m_pushbutton_show_orign_image, index, 0);

	m_pushbutton_detection = new QPushButton(this);
    m_pushbutton_detection->setFixedSize(ButtonSize);
    m_pushbutton_detection->setText("检测");
    connect(m_pushbutton_detection, &QPushButton::clicked, this, &FunctionScan::detectionImage);
	qgridLayout->addWidget(m_pushbutton_detection, index, 1);
    
    index++;
    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(ButtonSize);
    m_pushbutton_return->setText("退出");
    qgridLayout->addWidget(m_pushbutton_return, index, 0);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });
    loadParameter();
}

FunctionScan::~FunctionScan()
{
    ;
}
#include <vector>
#include <algorithm>
#include <iostream>

std::vector<size_t> get_sorted_positions(const std::vector<int>& vec) {
	// 第一步：获取排序索引
	std::vector<size_t> indices(vec.size());
	for (size_t i = 0; i < vec.size(); ++i) {
		indices[i] = i;
	}

	std::sort(indices.begin(), indices.end(),
		[&vec](size_t i, size_t j) { return vec[i] < vec[j]; });

	// 第二步：创建位置映射，表示每个原始元素在排序后的位置
	std::vector<size_t> positions(vec.size());
	for (size_t sorted_pos = 0; sorted_pos < indices.size(); ++sorted_pos) {
		size_t original_index = indices[sorted_pos];
		positions[original_index] = sorted_pos;
	}

	return positions;
}
bool FunctionScan::detection( HObject image_in, QString& message)
{
    
    Mat mat_image;


    try
    {

        HTuple htuple_size,htuple_length;
       blob9006(image_in, &m_region_rectangle,&m_region_show,&m_region_error, m_threshold_gray,m_threshold_error,m_dilation_x,m_dilation_y,m_height_min,m_height_max,120,m_empty_x,m_empty_y, m_empty_max_x, m_empty_max_y,3,m_empty_size,&htuple_size,&htuple_length);
        
        if (htuple_size.Length()<4)
        {

            message += "\n找到的块小于4";
            return false;
        }


		vector_error_index.clear();
        int value_total=0;
		vector<int> vector_value_tem;


        for (int i = 0; i < htuple_size.Length(); i++) {
            int value = htuple_size[i].I() ;
			vector_value_tem.push_back(value);
        }

		vector<size_t> indexs;
		indexs = get_sorted_positions(vector_value_tem);


        double max_double = *std::max_element(vector_value_tem.begin(), vector_value_tem.end());
        double min_double = *std::min_element(vector_value_tem.begin(), vector_value_tem.end());


        std::vector<double> vector_add;
        vector_add.push_back((max_double - min_double)*para_max);
        vector_add.push_back((max_double - min_double)*para_min);
        vector_add.push_back(-(max_double - min_double)*para_min);
        vector_add.push_back(-(max_double - min_double)*para_max);


		vector<int> vector_value;
        for (int i = 0; i < htuple_size.Length(); i++) {

            int value = htuple_size[i].I() ;

            //补偿
           value = value + vector_add[indexs[i]];

           value_total +=value;
			vector_value.push_back(value);
//            message += "\n像素面积" + QString::number(value);
            message +="\n百分比"+QString::number(value*100/m_standard_size);
			if (value < m_standard_size*m_ratio)
			{
				vector_error_index.push_back(i + 1);
//				message += "小于标准面积*比例"+QString::number(m_standard_size*m_ratio);

			}
			
		}
		if (vector_error_index.size() > 0)
		{
	
			return false;
		}

       message += "\n总像素面积" + QString::number(value_total)+"\n百分比"+QString::number(value_total*100/4/m_standard_size);
       if (value_total < 4 * m_standard_size * m_ratio)
       {
           message += "小于标准面积*比例"+QString::number(4*m_standard_size*m_ratio);
           return false;
       }


        HTuple hv_Length;
        CountObj(m_region_error, &hv_Length);
        int value = hv_Length[0].I();
        if(value>0)
        {
            message += "\n缺料";
            return false;
        }

        int length = htuple_length[0].I();
        if(length>4000)
        {
            message += "\n中间的块没找到";
            return false;
        }


    }
    catch (HException &HDevExpDefaultException)
    {
        message = HDevExpDefaultException.ErrorMessage();

        return false;
    }

    return true;
}

void FunctionScan::detectionImage()
{
    if(m_image.empty())
    {
        QMessageBox::information(nullptr,"","no image");
        return;
    }

    QString message;
    HObject image = matToHImage(m_image);
    bool state = detection(image, message);

	

    Mat image_show = m_image.clone();
    if(image_show.empty())
    {
        QMessageBox::information(nullptr,"","no image");
        return;
    }
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);

	if (state)
        m_view->setImage(q_image, message+"OK", Qt::green, 60);
	else
        m_view->setImage(q_image, message+"NG", Qt::red, 60);

    
    
    }

void FunctionScan::draw(Mat image_in,Mat& image_out)
{
    image_out = image_in;

    if (image_out.channels() == 1)
    {
        cvtColor(image_out, image_out, COLOR_GRAY2BGR);
    }

   
	image_out = drawRegion(image_out, m_region_rectangle, Scalar(0,255,0));


	

	/*for (int i=0;i<vector_error_index.size();i++)
	{
		HObject ho_ObjectSelected;
		SelectObj(m_region_rectangle, &ho_ObjectSelected, vector_error_index[i]);
		image_out = drawRegion(image_out, ho_ObjectSelected, Scalar(0, 0, 255));
	}*/

	GenEmptyRegion(&m_region_rectangle);

//    image_out = drawRegion(image_out, m_region_show, Scalar(0, 255, 0));



    GenEmptyRegion(&m_region_show);

    image_out = drawRegion(image_out, m_region_error, Scalar(0, 0, 255));



    GenEmptyRegion(&m_region_error);

}

void FunctionScan::loadParameter()
{
//    Row1 = m_qsettings->value(m_section + "/" + "Row1",10).toInt();
//    Column1 = m_qsettings->value(m_section + "/" + "Column1",10).toInt();
//    Row2 = m_qsettings->value(m_section + "/" + "Row2",5000).toInt();
//    Column2 = m_qsettings->value(m_section + "/" + "Column2",500).toInt();
//    m_view->setRectPose(Row1, Column1, Row2, Column2);
	para_min = m_qsettings->value(m_section + "/" + "para_min", 1).toDouble();
	para_max = m_qsettings->value(m_section + "/" + "para_max", 1).toDouble();

	
	m_standard_size = m_qsettings->value(m_section + "/" + "m_standard_size", 1).toInt();
	m_ratio = m_qsettings->value(m_section + "/" + "m_ratio", 1).toDouble();

    m_threshold_gray      = m_qsettings->value(m_section + "/" + "m_threshold_gray",100).toInt();
    m_threshold_error      = m_qsettings->value(m_section + "/" + "m_threshold_error",80).toInt();
    m_dilation_x = m_qsettings->value(m_section + "/" + "m_dilation_x",40).toInt();
    m_dilation_y = m_qsettings->value(m_section + "/" + "m_dilation_y",50).toInt();
    m_height_min = m_qsettings->value(m_section + "/" + "m_height_min",200).toInt();
    m_height_max = m_qsettings->value(m_section + "/" + "m_height_max", 300).toInt();

//    m_find_number = m_qsettings->value(m_section + "/" + "m_find_number", 110).toInt();
    m_empty_x = m_qsettings->value(m_section + "/" + "m_empty_x",50).toInt();
    m_empty_y = m_qsettings->value(m_section + "/" + "m_empty_y",50).toInt();

	m_empty_max_x = m_qsettings->value(m_section + "/" + "m_empty_max_x", 100).toInt();
    m_empty_max_y = m_qsettings->value(m_section + "/" + "m_empty_max_y", 100).toInt();
    m_empty_size = m_qsettings->value(m_section + "/" + "m_empty_size", 10000).toInt();

	m_qspinbox_standard_size->setValue(m_standard_size);
	m_qspinbox_ratio->setValue(m_ratio);

    m_qspinbox_threshold_gray->setValue(m_threshold_gray);
    m_qspinbox_threshold_error->setValue(m_threshold_error);
    m_qspinbox_dilation_x->setValue(m_dilation_x);
    m_qspinbox_dilation_y->setValue(m_dilation_y);
	m_qspinbox_height_min->setValue(m_height_min);
	m_qspinbox_height_max->setValue(m_height_max);
//    m_qspinbox_find_number->setValue(m_find_number);
    m_qspinbox_empty_x->setValue(m_empty_x);
    m_qspinbox_empty_y->setValue(m_empty_y);
	m_qspinbox_empty_max_x->setValue(m_empty_max_x);
	m_qspinbox_empty_max_y->setValue(m_empty_max_y);
    m_qspinbox_empty_size->setValue(m_empty_size);

    return ;
}

void FunctionScan::saveParameter()
{
	m_standard_size = m_qspinbox_standard_size->value();
	m_ratio = m_qspinbox_ratio->value();

    m_threshold_gray = m_qspinbox_threshold_gray->value();
    m_threshold_error = m_qspinbox_threshold_error->value();
    m_dilation_x = m_qspinbox_dilation_x->value();
    m_dilation_y = m_qspinbox_dilation_y->value();



    m_height_min = m_qspinbox_height_min->value();
    m_height_max = m_qspinbox_height_max->value();

//    m_find_number = m_qspinbox_find_number->value();

    m_empty_x = m_qspinbox_empty_x->value();
    m_empty_y = m_qspinbox_empty_y->value();
	m_empty_max_x = m_qspinbox_empty_max_x->value();
	m_empty_max_y = m_qspinbox_empty_max_y->value();
    m_empty_size = m_qspinbox_empty_size->value();

	m_qsettings->setValue(m_section + "/" + "m_standard_size", m_standard_size);

    m_qsettings->setValue(m_section + "/" + "m_ratio", m_ratio);

    m_qsettings->setValue(m_section + "/" + "m_threshold_gray", m_threshold_gray);
    m_qsettings->setValue(m_section + "/" + "m_threshold_error", m_threshold_error);
    m_qsettings->setValue(m_section + "/" + "m_dilation_x", m_dilation_x);
    m_qsettings->setValue(m_section + "/" + "m_dilation_y", m_dilation_y);

    m_qsettings->setValue(m_section + "/" + "m_height_min", m_height_min);
    m_qsettings->setValue(m_section + "/" + "m_height_max", m_height_max);
//    m_qsettings->setValue(m_section + "/" + "m_find_number", m_find_number);


    m_qsettings->setValue(m_section + "/" + "m_empty_x", m_empty_x);
    m_qsettings->setValue(m_section + "/" + "m_empty_y", m_empty_y);
	m_qsettings->setValue(m_section + "/" + "m_empty_max_x", m_empty_max_x);
	m_qsettings->setValue(m_section + "/" + "m_empty_max_y", m_empty_max_y);
    m_qsettings->setValue(m_section + "/" + "m_empty_size", m_empty_size);


    return;
}

bool FunctionScan::showDialog()
{
    QImage q_image = matToQImage(m_image);
    m_view->setImage(q_image, "",Qt::red, 20);
   
    return true;
}


